package dbTools

import (
	"fmt"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"vueProject/globalData"
	"vueProject/models/sys_db"
)

// var LocalDb *gorm.DB
var err error

func InitDb() interface{} {
	globalData.LocalDb, err = gorm.Open(sqlite.Open("Data.db"), &gorm.Config{})
	if err != nil {
		return map[string]interface{}{
			"code": -1,
			"msg":  err.Error(),
		}
	}
	err = globalData.LocalDb.AutoMigrate(&sys_db.SysConfig{}, &sys_db.DbConnectList{})
	if err != nil {
		return map[string]interface{}{
			"code": -1,
			"msg":  err.Error(),
		}
	}

	// 插入内容
	globalData.LocalDb.Create(&sys_db.SysConfig{Parameter: "测试项1", Value: "测试配置1", Remarks: "测试备注"})
	globalData.LocalDb.Create(&sys_db.SysConfig{Parameter: "测试项2", Value: "测试配置2", Remarks: "测试备注2"})
	//LocalDb.Create(&sys_db.DbConnectList{ConnectStr: "连接字符串1", Remarks: "连接字符串备注1"})

	// 读取内容
	//var product Product
	//db.First(&product, 1)                 // find product with integer primary key
	//db.First(&product, "code = ?", "D42") // find product with code D42
	//
	//// 更新操作：更新单个字段
	//db.Model(&product).Update("Price", 2000)
	//
	//// 更新操作：更新多个字段
	//db.Model(&product).Updates(Product{Price: 2000, Code: "F42"}) // non-zero fields
	//db.Model(&product).Updates(map[string]interface{}{"Price": 2000, "Code": "F42"})
	return map[string]interface{}{
		"code": 200,
		"msg":  "初始化成功",
	}
}

func checkErr(err error) {
	if err != nil {
		panic(err)
	}
}
func TestFunc(name string) string {
	return fmt.Sprintf("Hello %s, It's show time!", name)
}
